import time, random
from PIL import Image
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.by import By

chrome_browser = webdriver.Chrome()
chrome_browser.get(
    "https://passport.jd.com/new/login.aspx?ReturnUrl=https%3A%2F%2Fwww.jd.com%2F%3Fcu%3Dtrue%26utm_source%3Dbaidu-pinzhuan%26utm_medium%3Dcpc%26utm_campaign%3Dt_288551095_baidupinzhuan%26utm_term%3D0f3d30c8dba7459bb52f2eb5eba8ac7d_0_024dd11e7898482daae41e1cf6258dbb")
chrome_browser.maximize_window()
# 输入用户名和密码
control_login = chrome_browser.find_element(By.NAME, 'loginname')
control_login.send_keys('19559917806')
control_login1 = chrome_browser.find_element(By.ID, 'nloginpwd')
control_login1.send_keys('Gx583357384')
# 点击登入按钮
control_login2 = chrome_browser.find_element(By.XPATH, '//*[@id="loginsubmit"]')
control_login2.click()
time.sleep(2)
# 获取全屏图片并保存到本地
screenshot_all = chrome_browser.save_screenshot("screenshot_all.png")
# 6获取验证码位置
code_element = chrome_browser.find_element(By.XPATH,
                                           '//*[@id="JDJRV-wrap-loginsubmit"]/div/div/div/div[1]/div[2]/div[1]/img')
left = code_element.location['x']
top = code_element.location['y']
right = code_element.size['width'] + left
height = code_element.size['height'] + top
# 7读取本地验证码并截图验证码图片
im = Image.open("screenshot_all.png")
img = im.crop((left, top,right,height))
img.save("beforecode.png")
# 8滑块位置获取和点击滑块
botton = chrome_browser.find_element(By.XPATH, '//*[@id="JDJRV-wrap-loginsubmit"]/div/div/div/div[2]/div[3]')
botton.click()

screenshot_all = chrome_browser.save_screenshot("afterscreenshot_all.png")  # 获取全屏截图并保存到本地
#9
code_element = chrome_browser.find_element(By.XPATH,
                                           '//*[@id="JDJRV-wrap-loginsubmit"]/div/div/div/div[1]/div[2]/div[1]/img')
left = code_element.location['x']
top = code_element.location['y']
right = code_element.size['width'] + left
height = code_element.size['height'] + top
im = Image.open("afterscreenshot_all.png")
img = im.crop((left, top, right, height))
img.save("aftercode.png")
# 10
beforecode = Image.open("beforecode.png").convert("L")
aftercode = Image.open("aftercode.png").convert("L")
threshold = 60
width = beforecode.size[0]
height = beforecode.size[1]
# 11
mylocal = []
for h in range(0, height):
    for w in range(int(round(width / 3, 0)), width):
        beforepixel = beforecode.getpixel((w, h))
        afterpixel = aftercode.getpixel((w, h))
        if abs(beforepixel - afterpixel) > threshold:
            if w not in mylocal:
                distance = mylocal[0] / 1.5
                track = []
                current = 0
                mid = distance * 4 / 5
                t = 0.2
                v = 0
                while current < distance:
                    if current < mid:
                        a = 2
                    else:
                        a = -3
                    v0 = v
                    v = v0 + a * t
                    move = v0 * t + 1 / 2 * a * t * t

#13 移动滑块
actions = ActionChains(chrome_browser)
slider = chrome_browser.find_element(By.XPATH, '//*[@id="JDJRV-wrap-loginsubmit"]/div/div/div/div[2]/div[3]')
actions.click_and_hold(slider).perform()#点击

actions.move_by_offset(xoffset=69, yoffset=0).perform() #拖动滑块
time.sleep(1)
actions.release().perform()# 松开滑块
time.sleep(3)


